<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Store\Controller;
use Store\Controller\StoreController;
class SetingController extends StoreController{
    //put your code here
    function _initialize() {
        parent::_initialize();
        $this->assign('sys','seting');// 数据集

    }
    
    //账户详情
    public function index(){
        
        $this->assign('action','index');// 数据集
        $this->display();
    }

    //密码修改
    public function password(){
        //防止用户直接进入第二页
        if(I('post.page') == 2){
            if(!session('page')){
                $this->redirect('password');
            }
        }

        if(IS_POST){
            $p = I('post.');
            switch(I('post.page')){
                case 1 :
                    if(!$this->check_verify($p['seting'])){
                        $this->error('验证码不正确');
                    }
                    if(session('user_auth')['user_mobile'] != $p['user_mobile']){
                        $this->error('请检查绑定手机号');
                    }
                    if(session('mobile_code') == $p['mobile_code']){
                        $this->error('手机验证码不正确');
                    }

                    $pass = password($p['user_passwd'],session('user_auth')['user_salt']);

                    if(session('user_auth')['user_passwd'] != $pass[0]){
                        $this->error('密码不正确');
                    }
                    session('page',1);
                    $this->success('通过验证',U('password',array('page'=>2)));
                    break;
                case 2:
                    if($p['user_passwd'] == $p['queren_user_passwd']){
                        $password = password($p['user_passwd']);
                        $data['user_passwd'] = $password[0];
                        $data['user_salt'] = $password[1];
                        $a['user_id'] = session('user_id');
                        $re = M('users')->where($a)->save($data);
                        !$re ?$this->error('失败请稍后再试'):session(null);
                        $this->success('修改成功，请重新登陆',U('Login/index'));
                    }else{
                        $this->error('两次密码不一致');
                    }
            }
        }

        switch(I('page')){
            case 2:$myfrom[1] = "style='display:none;'";break;
            default : $myfrom[2]= "style='display:none;'";
        }
        $this->assign('myfrom',$myfrom);

        $this->assign('action','password');// 数据集
        $this->display();
    }

    // 生成验证码
    public function verify(){
        $config =    array(
            'fontSize'    =>    24,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'imageW'      =>    0,     // 验证码宽度
            'imageH'      =>    0,     // 验证码高度
            'fontttf'     =>   '1.ttf',
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 验证验证码
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }
}
